<template>
    <el-dialog
        v-model="dialogVisible"
        title="新建模板"
        width="600px"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        class="new-template-dialog"
        @close="handleClose"
    >
        <div class="mode-selection">
          <div class="mbflmc-row">
            <span class="mbflmc-label">模板分类：</span>
            <span class="mbflmc-value">{{ mbflmc }}</span>
          </div>
            <div class="mode-title">选择模式</div>
            <div class="mode-buttons">
                <div
                    class="mode-button"
                    :class="{ active: selectedMode === 'direct' }"
                    @click="selectMode('direct')"
                >
                    <div class="mode-icon">T</div>
                    <div class="mode-text">直接编辑</div>
                </div>
                <div
                    class="mode-button"
                    :class="{ active: selectedMode === 'upload' }"
                    @click="selectMode('upload')"
                >
                    <div class="mode-icon">W</div>
                    <div class="mode-text">上传word模板</div>
                </div>
            </div>
        </div>

        <!-- 文件显示区域 -->
        <div v-if="selectedMode === 'upload' && uploadedFile" class="file-display">
            <div class="file-item">
                <div class="file-icon">📄</div>
                <div class="file-name">{{ uploadedFile.name }}</div>
                <div class="file-remove" @click="removeFile">
                    <el-icon><Close /></el-icon>
                </div>
            </div>
        </div>

        <!-- 文件上传区域 -->
        <div v-if="selectedMode === 'upload' && !uploadedFile" class="upload-area">
            <el-upload
                ref="uploadRef"
                :auto-upload="false"
                :show-file-list="false"
                accept=".doc,.docx"
                @change="handleFileChange"
            >
                <div class="upload-content">
                    <el-icon class="upload-icon"><Upload /></el-icon>
                    <div class="upload-text">点击上传Word文档</div>
                </div>
            </el-upload>
        </div>

      <!-- 直接编辑选择编辑模式显示区域 -->
      <div v-if="selectedMode === 'direct'" class="file-display">
        <el-radio-group v-model="editMode">
          <el-radio value="2" size="large">在线office</el-radio>
          <el-radio value="1" size="large" >富文本</el-radio>
        </el-radio-group>
      </div>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="handleCancel">取消</el-button>
                <el-button type="primary" @click="handleConfirm" :disabled="!canConfirm">下一步</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import { Close, Upload } from '@element-plus/icons-vue'

export default {
    name: 'NewTemplateDialog',
    components: {
        Close,
        Upload
    },
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        mbflmc: {
          type: String,
          default: ''
        }
    },
    emits: ['update:visible', 'confirm'],
    data() {
        return {
            selectedMode: 'direct', // 'direct' 或 'upload'
            uploadedFile: null,
            editMode:'2'
        }
    },
    computed: {
        dialogVisible: {
            get() {
                return this.visible
            },
            set(value) {
                this.$emit('update:visible', value)
            }
        },
        canConfirm() {
            if (this.selectedMode === 'direct') {
                return true;
            } else if (this.selectedMode === 'upload') {
                return this.uploadedFile !== null;
            }
            return false;
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                this.resetForm()
            }
        }
    },
    methods: {
        selectMode(mode) {
            this.selectedMode = mode;
            if (mode === 'direct') {
                this.uploadedFile = null;
            }
        },
        handleFileChange(file) {
            this.uploadedFile = file;
        },
        removeFile() {
            this.uploadedFile = null;
        },
        handleCancel() {
            this.dialogVisible = false;
            this.resetForm();
        },
        handleConfirm() {
            if (this.selectedMode === 'direct') {
                this.$emit('confirm', { mode: 'direct',editMode: this.editMode });
                this.dialogVisible = false;
            } else if (this.selectedMode === 'upload') {
                this.$emit('confirm', { mode: 'upload', file: this.uploadedFile });
                this.dialogVisible = false;
            }
        },
        handleClose() {
            this.resetForm();
        },
        resetForm() {
            this.selectedMode = 'direct';
            this.uploadedFile = null;
        }
    }
}
</script>

<style scoped>
/* 新建模板弹窗样式 */
:deep(.new-template-dialog .el-dialog__header) {
    padding: 20px 20px 10px 20px;
    border-bottom: 1px solid #e4e7ed;
}

:deep(.new-template-dialog .el-dialog__title) {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

:deep(.new-template-dialog .el-dialog__body) {
    padding: 20px;
}

:deep(.new-template-dialog .el-dialog__footer) {
    padding: 10px 20px 20px 20px;
    border-top: 1px solid #e4e7ed;
}

.mode-selection {
    margin-bottom: 20px;
}

.mbflmc-row {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  gap: 6px;
}
.mbflmc-label {
  color: #888;
  font-size: 14px;
}
.mbflmc-value {
  color: #2567E9;
  font-size: 15px;
  font-weight: 500;
}

.mode-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 15px;
}

.mode-buttons {
    display: flex;
    gap: 20px;
}

.mode-button {
    flex: 1;
    height: 120px;
    border: 2px solid #e4e7ed;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    background: #fafafa;
}

.mode-button:hover {
    border-color: #2567E9;
    background: #f0f8ff;
}

.mode-button.active {
    border-color: #2567E9;
    background: #f0f8ff;
}

.mode-icon {
    font-size: 32px;
    font-weight: bold;
    color: #2567E9;
    margin-bottom: 10px;
}

.mode-text {
    font-size: 14px;
    color: #333;
    font-weight: 500;
}

.file-display {
    margin-top: 15px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 6px;
    border: 1px solid #e4e7ed;
}

.file-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.file-icon {
    font-size: 20px;
    color: #2567E9;
}

.file-name {
    flex: 1;
    font-size: 14px;
    color: #333;
}

.file-remove {
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    color: #999;
    transition: all 0.2s ease;
}

.file-remove:hover {
    background: #f0f0f0;
    color: #f56c6c;
}

.upload-area {
    margin-top: 15px;
}

.upload-content {
    border: 2px dashed #d9d9d9;
    border-radius: 6px;
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
}

.upload-content:hover {
    border-color: #2567E9;
    background: #f0f8ff;
}

.upload-icon {
    font-size: 32px;
    color: #999;
    margin-bottom: 10px;
}

.upload-text {
    font-size: 14px;
    color: #666;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

:deep(.new-template-dialog .el-button) {
    padding: 8px 20px;
    font-size: 14px;
}

:deep(.new-template-dialog .el-button--primary) {
    background: #2567E9;
    border-color: #2567E9;
}

:deep(.new-template-dialog .el-button--primary:hover) {
    background: #1e5fd9;
    border-color: #1e5fd9;
}
</style>
